/*
 * Copyright (C) 2008 Rob Cash
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * 	http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ScheduleEditorWizardPanel.java
 */
package org.robcash.apps.messagepopup.view.wizard;

import static org.robcash.apps.messagepopup.BeanNameConstants.SCHEDULE_BEAN_NAME;

import org.robcash.apps.messagepopup.Main;
import org.robcash.apps.messagepopup.view.components.ScheduleEditor;
import org.robcash.apps.messagepopup.vo.Schedule;
import org.robcash.commons.swing.wizard.Wizard;
import org.robcash.commons.swing.wizard.impl.BaseWizardPanel;
import org.robcash.commons.swing.wizard.model.WizardModel;
import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;

/**
 * Wizard panel for editing a schedule
 * @author Rob
 */
public class ScheduleEditorWizardPanel extends BaseWizardPanel<ScheduleEditor> {

    /**
     * Schedule editing component
     */
    private ScheduleEditor editor;
    
    /**
     * Create a new schedule editor wizard panel
     * @param objectId Object id of this panel
     */
    public ScheduleEditorWizardPanel(String objectId) {
        super(objectId);
        editor = new ScheduleEditor();
        setComponent(editor);
    }
    
    /**
     * Indicates that the wizard panel is about to be displayed
     * @param wizard Wizard that is about to display this panel
     */
    @Override
    public void aboutToDisplay(Wizard wizard) {
        super.aboutToDisplay(wizard);
        SubscriptionModel model = (SubscriptionModel) wizard.getModel();
        Schedule schedule = model.getSchedule();
        model.setScheduleIsNew(schedule == null);
        if ( schedule == null ) {
            GenericBeanFactoryAccessor beanFactory = Main.getApplication().getSpringContext();
            schedule = beanFactory.getBean(SCHEDULE_BEAN_NAME);
        }
        editor.setSchedule(schedule);
    }

    /**
     * Indicates that the wizard panel is about to be displayed
     * @param wizard Wizard that is about to hide this panel
     * @param saveWork Indicates whether or not work should be saved
     */
    @Override
    public void aboutToHide(Wizard wizard, boolean saveWork) {
        // Save settings into a schedule
        if ( saveWork ) {
            SubscriptionModel model = (SubscriptionModel) wizard.getModel();
            editor.applyPendingChanges();
            model.setSchedule(editor.getSchedule());
        }

        super.aboutToHide(wizard, saveWork);
    }

    @Override
    public boolean canGoForward() {
        return true;
    }

    @Override
    public String getNextPanelId() {
        return WizardModel.NEXT;
    }

}
